package com.tql.pandian.util;

import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.utils.CalculatedValueUtil;
import com.tql.pandian.entity.TqlStapOutChange;
import com.tql.pandian.entity.TqlStapOutChangeBomInfo;
import com.tql.pandian.entity.dto.SimpleDto;

import java.math.BigDecimal;

public class BusinessUtil {

    public static BigDecimal getInNum(SimpleDto unionE) {
        BigDecimal incase=null==unionE.getIncaseNum()?BigDecimal.ZERO:unionE.getIncaseNum();
        BigDecimal purcharse=null==unionE.getPurcharseNum()?BigDecimal.ZERO:unionE.getPurcharseNum();
        BigDecimal transferIn=null==unionE.getTransferInNum()?BigDecimal.ZERO:unionE.getTransferInNum();
        BigDecimal transferOut=null==unionE.getTransferOutNum()?BigDecimal.ZERO:unionE.getTransferOutNum();
        BigDecimal b1= CalculatedValueUtil.additionDecimal(incase,purcharse);
        BigDecimal b2= CalculatedValueUtil.additionDecimal(transferIn,b1);
        BigDecimal b3= CalculatedValueUtil.subtractionBigDecimal(b2,transferOut);
        return b3;
    }

    public static BigDecimal getInAmout(SimpleDto unionE) {
        BigDecimal incase=null==unionE.getIncaseAmount()?BigDecimal.ZERO:unionE.getIncaseAmount();
        BigDecimal purcharse=null==unionE.getPurcharseAmount()?BigDecimal.ZERO:unionE.getPurcharseAmount();
        BigDecimal transferIn=null==unionE.getTransferInAmount()?BigDecimal.ZERO:unionE.getTransferInAmount();
        BigDecimal transferOut=null==unionE.getTransferOutAmount()?BigDecimal.ZERO:unionE.getTransferOutAmount();
        BigDecimal b1= CalculatedValueUtil.additionDecimal(incase,purcharse);
        BigDecimal b2= CalculatedValueUtil.additionDecimal(transferIn,b1);
        BigDecimal b3= CalculatedValueUtil.subtractionBigDecimal(b2,transferOut);
        return b3;
    }

    /**
     *  成本（数量）=期初+采购-期末
     * @param lastPandianAmount
     * @param inNum
     * @param thisPandianAmount
     * @return
     */
    public static BigDecimal getRealCost(BigDecimal lastPandianAmount, BigDecimal inNum, BigDecimal thisPandianAmount) {
        BigDecimal lastPandianAmount1=null==lastPandianAmount?BigDecimal.ZERO:lastPandianAmount;
        BigDecimal inNum1=null==inNum?BigDecimal.ZERO:inNum;
        BigDecimal thisPandianAmount1=null==thisPandianAmount?BigDecimal.ZERO:thisPandianAmount;

        BigDecimal b2= CalculatedValueUtil.additionDecimal(lastPandianAmount1,inNum1);
        BigDecimal b3= CalculatedValueUtil.subtractionBigDecimal(b2,thisPandianAmount1);
        return b3;

    }

    public static BigDecimal getRealCostAmount(BigDecimal lastPandianAmount, BigDecimal lastPandianPrice, BigDecimal inAmout, BigDecimal thisPandianAmount, BigDecimal thisPandianPrice) {

        BigDecimal lastPandianAmount1=null==lastPandianAmount?BigDecimal.ZERO:lastPandianAmount;
        BigDecimal lastPandianPrice1=null==lastPandianPrice?BigDecimal.ZERO:lastPandianPrice;
        BigDecimal inAmout1=null==inAmout?BigDecimal.ZERO:inAmout;
        BigDecimal thisPandianAmount1=null==thisPandianAmount?BigDecimal.ZERO:thisPandianAmount;
        BigDecimal thisPandianPrice1=null==thisPandianPrice?BigDecimal.ZERO:thisPandianPrice;
       BigDecimal lastAmount= CalculatedValueUtil.multiplyB(lastPandianPrice1,lastPandianAmount1);
       BigDecimal thisAmount=CalculatedValueUtil.multiplyB(thisPandianPrice1,thisPandianAmount1);
       BigDecimal b1=CalculatedValueUtil.additionDecimal(lastAmount,inAmout1);
        BigDecimal b3= CalculatedValueUtil.subtractionBigDecimal(b1,thisAmount);
        return b3;
    }

    public static BigDecimal getProlossNum(BigDecimal inNum1,BigDecimal outNum) {
        BigDecimal inNum11=null==inNum1?BigDecimal.ZERO:inNum1;
        BigDecimal outNum11=null==outNum?BigDecimal.ZERO:outNum;
     return    CalculatedValueUtil.subtractionBigDecimal(inNum11, outNum11);
    }

    public static BigDecimal getProlossAmout(BigDecimal inAmout1, BigDecimal outAmout) {
        BigDecimal inAmout11=null==inAmout1?BigDecimal.ZERO:inAmout1;
        BigDecimal outAmout11=null==outAmout?BigDecimal.ZERO:outAmout;
        return CalculatedValueUtil.subtractionBigDecimal(inAmout11, outAmout11);
    }




    public static TqlStapOutChange getOutDetail(TqlStapOutChangeBomInfo bomInfo, TqlStapOutChange outChange) {
        TqlStapOutChange dto =new TqlStapOutChange();
        dto.setShopId(outChange.getShopId());
        dto.setFoodCode(outChange.getFoodCode());
        dto.setFoodName(outChange.getFoodName());
        dto.setReportDate(outChange.getReportDate());
        dto.setDeptId(bomInfo.getDeptId());
        dto.setStapCode(bomInfo.getStapCode());
        dto.setStapName(bomInfo.getStapName());
        BigDecimal sumquota= CalculatedValueUtil.multiplyB(bomInfo.getQuota(),outChange.getFoodCount());
        dto.setSumquota(sumquota);
        BigDecimal sumcost=CalculatedValueUtil.multiplyB(bomInfo.getCost(),outChange.getFoodCount());
        dto.setSumcost(sumcost);
        dto.setDeptCode(bomInfo.getDeptCode());
        dto.setOriginId(outChange.getId());
        return dto;
    }


    /**
     * 通过物料的情况判断成本卡偏大还是偏小
     * @param inNum1
     * @param outNum
     * @return
     */
    public static int getBomParse(BigDecimal inNum1, BigDecimal outNum) {
        inNum1 = inNum1.setScale(0, BigDecimal.ROUND_DOWN);
        outNum = outNum.setScale(0, BigDecimal.ROUND_DOWN);
        int result = inNum1.compareTo(outNum);
        //实际成本大于理论成本  返回2 成本卡偏小
        if (result > 0) {
            return 2;
        }
        //实际成本小于理论成本  返回1 成本卡偏大
        else if (result < 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public static BigDecimal getFoodProfit(BigDecimal sumAmount,BigDecimal sumAmountBom) {

       return  CalculatedValueUtil.subtractionBigDecimal(sumAmount, sumAmountBom);
    }

    public static BigDecimal getFoodProfitMargin(BigDecimal sumAmount,BigDecimal sumAmountBom) {
        if(sumAmountBom.equals(BigDecimal.ZERO))
        {
            return new BigDecimal(100);
        }
        else {

            return CalculatedValueUtil.division(sumAmount, sumAmountBom);

        }


    }
}
